# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def title(content)
  	content_for(:title) { content }
  end
  
  def javascript(*args)
  	content_for(:head) { javascript_include_tag(*args) }
	end
	
	def stylesheet(*args)
		content_for(:head) { stylesheet_link_tag(*args) }
	end
  
  def text_teaser(txt, length, more_link = "")
  	txt = txt.no_html
  	cuted = txt[/(.|\n){0,#{length}}[\w]*/]
		cuted += more_link if(cuted != txt)
		cuted
  end
  
  def path(cat)
  	return '#' if cat.nil?
  	unless cat.root?
  		path = cat.ancestors.map(&:slug)
  		path.delete(cat.slug)
  		path = path.join('/')
  		path.blank? ? "/#{cat.slug}" : "/#{path}/#{cat.slug}"
  	else
  		category_path(cat)
  	end
  end
  
  def block_form(options={}, &block)
    width = options[:width]
    
  	concat(tag('div', {:class=>"block dark", :style=>"margin: 10px 0px;width:#{width}px;"}, true))
  	concat('<b class="rd5"></b><b class="rd4"></b><b class="rd3"></b><b class="rd2"></b><b class="rd1"></b>')
  	concat(tag('div', {:class=>'content'}, true))
  	yield
  	concat("</div>")
  	concat('<b class="rd1"></b><b class="rd2"></b><b class="rd3"></b><b class="rd4"></b><b class="rd5"></b>')
  	concat("</div>")
  end

  def head_options(record, options={})
    return if record.nil?
    
    header = nil
    header = record.header if record.respond_to?(:header)
    
    # title
    page_title = []
		page_title << options[:title] if options.key?(:title)
		page_title << ((header.nil? || header.title.blank?) ? record.title : header.title)
		page_title << t('page.title') if options[:append_title]
		page_title.flatten!
		page_title.compact!
		page_title.uniq!
		content_for :title, page_title.join(" | ")
		
		# keywords
		keywords = record.keywords.join(', ') if record.respond_to?(:keywords)
		keywords ||= (header.nil? || header.keywords.blank?) ? page_title.join(' ').split.join(', ') : header.keywords
		content_for :keywords, tag("meta", :content=>keywords, :name=>"keywords")
		
		# description
		description = (header.nil? || header.description.blank?) ? page_title.join(" - ") : header.description
		content_for :description, tag("meta", :content=>description, :name=>"description")
  end
end
